<?php
/**
****配送方式
**/
class ShopshipAction extends Action
{
	public function preMethod()
	{
		R("Mayijie/check_admin");
		$perm_key = session('admin_perm');
		$perm_arr = explode(",",$perm_key);
		if(!in_array(2,$perm_arr)){
			$this->error("无权操作");
		}
	}

	public function index(){
		$Express=M('Express');
		$this->express=$Express->order('sorting')->select();
		$this->display();
	}

	public function add(){
		$Area=M('Area');
		$this->area=$Area->where('area_grade=1')->select();
		$this->display();
	}

	public function add_ex(){
		$Express=M('Express');
		$Express2=M('Express_value');

		$data['ex_name']=$this->_post('name');
		$data['one_w']=$this->_post('one_w');
		$data['two_w']=$this->_post('two_w');
		$data['onefee']=$this->_post('onefee');
		$data['twofee']=$this->_post('twofee');
		$data['intro']=$this->_post('intro');
		$type=$this->_post('type');

		if(empty($data['ex_name'])){
			$this->error("名称不得为空！");
		}
		if($type==3){
			$data['is_delivery']='false';
		}else{
			$data['is_delivery']='true';
		}

		$data['type']=$type;
	    $ex_id=$Express->add($data);

		if($type==2){
		   $area=$this->_post('area');
		   $one_fee=$this->_post('one_fee');
		   $two_fee=$this->_post('two_fee');
		   for($i=0;$i<count($one_fee);$i++){
			   if(!empty($one_fee[$i])){
				   $data2['ex_id']=$ex_id;
				   $data2['ex_area']=$area[$i];
				   $data2['onefee']=$one_fee[$i];
				   $data2['twofee']=$two_fee[$i];
				   $Express2->add($data2);
			   }
		   }
		}
		//if end
		$this->success("添加成功！");
	}

	public function remove(){
		$Express=M('Express');
		$Value=M('Express_value');
		$Tpl=M('Express_tpl');

		$map['id']=$this->_get('id');
		$map2['ex_id']=$map['id'];
		$Express->where($map)->delete();
		$Value->where($map2)->delete();
		$Tpl->where($map2)->delete();
		echo 1;
    }

	//排序
	public function sorting(){
		$Ship=M('Express');
		$sid=$this->_post('ship');
		$sort=$this->_post('sort');
		for($i=0;$i<count($sid);$i++){
			$data['sorting']=$sort[$i];
			$Ship->where('id='.$sid[$i])->save($data);
		}
	}

	//配送时间
	public function dlytime(){
		$Time=M('Dlytime');
		$action=$this->_get('action');
		if($action=='add'){
			$data['time']=$this->_post('time');
			if($Time->where($data)->select()){
				$this->error("此时间已经存在！");
			}
			if($Time->add($data)){
				$this->success("添加成功！");
			}else{
				$this->error("添加失败！");
			}
		}else{
			$this->dlytime=$Time->select();
			$this->display();
		}
	}

	//配送时间删除
	public function del_dlytime(){
		$Time=M('Dlytime');
		$map['id']=$this->_get('id');
		if($Time->where($map)->delete()){
			$this->success("删除成功！");
		}else{
			$this->error("删除失败！");
		}
	}

	//快递打印模板
	public function ex_tpl(){
		$Express=M('Express');
		$Tpl=M('Express_tpl');
		$this->express=$Express->order('sorting')->select();
		$this->tpl=$Tpl->join('e_express ON e_express.id=e_express_tpl.ex_id')->select();
		$this->display();
	}
	public function add_tpl(){
		$Tpl=M('Express_tpl');
		$ex_id=$this->_post('exp');
		$data['tpl']=stripslashes(htmlspecialchars_decode($this->_post('tpl')));
		$ck=$Tpl->where('ex_id='.$ex_id)->select();
		if($ck){
			$this->error("已经存在！");
		}else{
			$data['ex_id']=$ex_id;
			$re=$Tpl->add($data);
			if($re){$this->success("添加成功！");}
		}
	}
	public function edit_tpl(){
		$id=$this->_get('eid');
		$Tpl=M('Express_tpl');
		$this->tpl=$Tpl->where('ex_id='.$id)->find();
		$this->display();
	}
	public function save_tpl(){
		$id=$this->_post('eid');
		$data['tpl']=stripslashes(htmlspecialchars_decode($this->_post('tpl')));
		$Tpl=M('Express_tpl');
		$re=$Tpl->where('ex_id='.$id)->save($data);
		if($re){
			$this->success("修改成功！");
		}else{
			$this->error("修改失败！");
		}
	}

}

?>